package com.eims.ball.info.adapter;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.contans.view.FenxiJishuVo;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;
import bls.juejin.cn.R;

public class FanxiJishuShowAdapter extends BaseAdapter {
	private Context context;
	private int width;

	private FenxiJishuVo fenxiJishuVo;

	private String title, teamA, teamB;
	private String[] titles, teamAs, teamBs;

	public FanxiJishuShowAdapter(Context context, int width, FenxiJishuVo fenxiJishuVo) {
		super();
		this.context = context;
		this.width = width;
		this.fenxiJishuVo = fenxiJishuVo;
		if (fenxiJishuVo != null) {
			title = fenxiJishuVo.getTitle();
			teamA = fenxiJishuVo.getTeam_A();
			teamB = fenxiJishuVo.getTeam_B();
			if (title != null && !title.equals("")) {
				titles = title.split(",");
			}
			if (teamA != null && !teamA.equals("")) {
				teamAs = teamA.split(",");
			}
			if (teamB != null && !teamB.equals("")) {
				teamBs = teamB.split(",");
			}

		}

	}

	@Override
	public int getCount() {
		if (titles != null) {
			return titles.length;
		}

		return 0;
	}

	@Override
	public Object getItem(int position) {

		return position;
	}

	@Override
	public long getItemId(int position) {

		return position;
	}

	@Override
	public View getView(final int position, View view, ViewGroup parent) {
		ViewHolder holder;
		// 判断View是否为空
		if (view == null) {
			holder = new ViewHolder();

			view = LayoutInflater.from(context).inflate(R.layout.fenxi_jishu_list_adapter_item, null);
			
			holder.rlJishuShuju = (RelativeLayout) view.findViewById(R.id.rl_top_info);
			holder.lrJishuShuju = (LinearLayout) view.findViewById(R.id.lr_jishu_shuju);
			holder.tv_main_info = (TextView) view.findViewById(R.id.tv_main_info);
			holder.tv_info_whole = (TextView) view.findViewById(R.id.tv_info_whole);
			holder.tv_gues_info = (TextView) view.findViewById(R.id.tv_gues_info);
			holder.viewMain = view.findViewById(R.id.view_main);
			holder.viewGues = view.findViewById(R.id.view_gues);

			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}
		holder.tv_info_whole.setText(titles[position]);

		if (teamAs[position].equals("无数据")) {
			holder.tv_main_info.setText("--");
		} else {
			holder.tv_main_info.setText(teamAs[position]);
		}
		if (teamBs[position].equals("无数据")) {
			holder.tv_gues_info.setText("--");
		} else {
			holder.tv_gues_info.setText(teamBs[position]);
		}

		// holder.tv_gues_info.setText(teamBs[position]);

		String mainNum = getStringwihtOut(teamAs[position]);
		String guesNum = getStringwihtOut(teamBs[position]);
		Double mainDu = 0.0;

		Double guesDu = 0.0;
		if (mainNum.contains("%")) {
			mainDu = Double.parseDouble((mainNum.split("%")[0]));
		} else {
			if (mainNum.equals("")) {
				mainDu = Double.parseDouble("0");
			} else {
				mainDu = Double.parseDouble(mainNum);
			}

		}
		if (guesNum.contains("%")) {
			guesDu = Double.parseDouble((guesNum.split("%")[0]));
		} else {
			if (guesNum.equals("")) {
				guesDu = Double.parseDouble("0");
			} else {
				guesDu = Double.parseDouble(guesNum);
			}

		}
		LinearLayout.LayoutParams ppl = (LayoutParams) holder.lrJishuShuju.getLayoutParams();
		ppl.width = width - 80;

		ppl.setMargins(40,10, 40, 0);

		LinearLayout.LayoutParams pp2 = (LayoutParams) holder.rlJishuShuju.getLayoutParams();
		pp2.width = width - 80;

		pp2.setMargins(40, 60, 40, 0);
		
		LinearLayout.LayoutParams pp = (LayoutParams) holder.viewMain.getLayoutParams();
		Double whole = Double.parseDouble((width - 80) + "");
		Log.e("System.out", mainDu + "-----" + guesDu);
		if (mainDu == 0) {
			if (guesDu == 0) {
				pp.width = (int) ((whole - 15) / 2);

				pp.setMargins(0, 0, 15, 0);
				holder.viewMain.setLayoutParams(pp);// 把banner图的高度设为此宽度值
				holder.viewMain.setBackgroundResource(R.color.main_peilv);
				holder.viewGues.setBackgroundResource(R.color.main_peilv);

			} else {
				pp.width = 0;

				pp.setMargins(0, 0, 0, 0);
				holder.viewMain.setLayoutParams(pp);// 把banner图的高度设为此宽度值
				holder.viewMain.setBackgroundResource(R.color.main_peilv);
				holder.viewGues.setBackgroundResource(R.color.main_gues_info);
			}
		} else {
			if (guesDu == 0) {
				pp.width = width - 80;

				pp.setMargins(0, 0, 0, 0);
				holder.viewMain.setLayoutParams(pp);// 把banner图的高度设为此宽度值
				holder.viewMain.setBackgroundResource(R.color.main_red);
				holder.viewGues.setBackgroundResource(R.color.main_peilv);

			} else {
				pp.width = (int) (((whole-15) / (mainDu + guesDu)) * (mainDu));

				pp.setMargins(0, 0, 15, 0);
				holder.viewMain.setLayoutParams(pp);// 把banner图的高度设为此宽度值
				holder.viewMain.setBackgroundResource(R.color.main_red);
				holder.viewGues.setBackgroundResource(R.color.main_gues_info);
			}
		}

		Log.e("System.out", pp.width + "-----");

		return view;
	}

	public boolean isMobileNO(String mobiles) {
		Pattern p = Pattern.compile("1[0-9]{10}");
		Matcher m = p.matcher(mobiles);
		return m.matches();
	}

	static class ViewHolder {
		RelativeLayout rlJishuShuju;
		LinearLayout lrJishuShuju;
		TextView tv_main_info, tv_info_whole, tv_gues_info;
		View viewMain, viewGues;

	}

	public String getStringwihtOut(String input) {
		String regex = "[\\u4e00-\\u9fa5]";
		Pattern pat = Pattern.compile(regex);
		Matcher mat = pat.matcher(input);
		String repickStr = mat.replaceAll("");
		return repickStr;

	}

	public String formateRate(String rateStr) {
		if (rateStr.indexOf(".") != -1) {

			int num = 0;
			num = rateStr.indexOf(".");

			String dianAfter = rateStr.substring(0, num + 1);
			String afterData = rateStr.replace(dianAfter, "");
			if (afterData.length() < 2) {
				afterData = afterData + "0";
			} else {
				afterData = afterData;
			}
			return rateStr.substring(0, num) + "." + afterData.substring(0, 2);
		} else {
			if (rateStr == "1") {
				return "100";
			} else {
				return rateStr;
			}
		}
	}

}
